﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RibbonEditor.Forms.Rules
{
    public partial class RulePicker : Form
    {
        XmlNode rulesNode;

        public string SelectedRule { get; set; }

        MainForm linkToMainForm;

        public RulePicker(XmlNode rulesNode, MainForm mainForm)
        {
            InitializeComponent();
            this.linkToMainForm = mainForm;
            this.rulesNode = rulesNode;
            this.FillList();

            object o = this.ParentForm;
        }

        private void FillList()
        {
            this.lstItems.Items.Clear();

            foreach (XmlNode ruleNode in this.rulesNode.ChildNodes)
            {
                ListViewItem item = new ListViewItem(ruleNode.Attributes["Id"].Value);
                if (ruleNode.FirstChild != null)
                {
                    item.SubItems.Add(ruleNode.FirstChild.Name);
                }

                item.Tag = ruleNode;
                this.lstItems.Items.Add(item);
            }
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                this.SelectedRule = node.Attributes["Id"].Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Please select a rule!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.SelectedRule = null;
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                CreateRuleDialog dialog = new CreateRuleDialog(node, null);
                dialog.StartPosition = FormStartPosition.CenterParent;
                dialog.ShowDialog();
            }
        }

        private void btnNewRule_Click(object sender, EventArgs e)
        {
            TreeView mainTv = ((TreeView)this.linkToMainForm.Controls.Find("",true)[0]);
            
            CreateRuleDialog cllDialog = new CreateRuleDialog(this.rulesNode, mainTv);
            cllDialog.StartPosition = FormStartPosition.CenterParent;

            if (cllDialog.ShowDialog() == DialogResult.OK)
            {
                this.FillList();
                this.linkToMainForm.RefreshRules();
            }
        }

        private void lstItems_DoubleClick(object sender, EventArgs e)
        {
            if (this.lstItems.SelectedItems.Count > 0)
            {
                XmlNode node = (XmlNode)this.lstItems.SelectedItems[0].Tag;
                this.SelectedRule = node.Attributes["Id"].Value;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
